
package io.gitee.hefren.hhcache.command;

import com.google.common.base.Strings;
import io.gitee.hefren.hhcache.core.Command;
import io.gitee.hefren.hhcache.core.CommandEnum;
import io.gitee.hefren.hhcache.core.HhCache;
import io.gitee.hefren.hhcache.core.Reply;

/**
 * @Date 2024/7/9
 * @Author lifei
 */
public class DecrCommand implements Command {
    @Override
    public String name() {
        return CommandEnum.DECR.name();
    }

    @Override
    public Reply<?> exec(HhCache cache, String[] args) {
        String key = getKey(args);
        try {
            int num = cache.decr(key);
            return Reply.integerReply(num);
        }catch (NumberFormatException e) {
            String message = Strings.lenientFormat("NFE key: %s, valud is not integer", key);
            return Reply.errorReply(message);
        }
    }
}
